package com.hspedu.newData.dmsxl.动态规划.左右数字;

import java.util.Arrays;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-02 
 * @Version: 1.0
 */

public class LC838推多米诺 {

    public String pushDominoes(String dominoes) {

        int n = dominoes.length();
        int[] closestL = new int[n];
        Arrays.fill(closestL, Integer.MAX_VALUE);

        int L = Integer.MAX_VALUE;
        for (int i = n - 1; i >= 0; i--) {
            if (dominoes.charAt(i) == 'L') {
                L = i;
            } else if (dominoes.charAt(i) == 'R') {
                L = Integer.MAX_VALUE;
            }
            if (L < Integer.MAX_VALUE) closestL[i] = L - i;
        }

        int R = Integer.MAX_VALUE;
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (dominoes.charAt(i) == 'R') {
                R = i;
            } else if (dominoes.charAt(i) == 'L') {
                R = Integer.MAX_VALUE;
            }
            int closestR = R < Integer.MAX_VALUE ? i - R : Integer.MAX_VALUE;
            if (closestR < closestL[i]) ans.append('R');
            else if (closestR > closestL[i]) ans.append('L');
            else ans.append('.');
        }
        return ans.toString();

    }
}
